package infopreserve.main.mobile;


import infopreserve.main.mobile.R;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;


public class AllFiles extends ListActivity {
	public String[] items;
	public CharSequence filename;
	protected Dialog dialog;
	
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	if(items == null)
    	newListView(files,R.layout.file);
    	else
    		newListView(items,R.layout.file);
    }
    	public void newListView(String[] items, int v){
    	setListAdapter(new ArrayAdapter<String>(this, v, items));

    	  ListView lv = getListView();
    	  lv.setTextFilterEnabled(true);

    	  lv.setOnItemClickListener(new OnItemClickListener() {
    	    public void onItemClick(AdapterView<?> parent, View view,
    	        int position, long id) {
    	    	filename = ((TextView)view).getText();
    	    	onButtonPopup(view);
    	    }
    	  });
    }

    
    final String[] files = new String[]{
        	"name","file","file","file","file","file","file","file","file","file","file","file","file","file","file"
        };
    
    public void onButtonPopup (final View target) {
    	dialog = new Dialog(AllFiles.this);
    	dialog.setContentView(R.layout.detail);
    	dialog.setTitle(filename);
    	final Button button = (Button) dialog.findViewById(R.id.button1);
    	Button b = (Button) dialog.findViewById(R.id.close);
    	dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    	dialog.show();

    	button.setOnClickListener(new View.OnClickListener() {
    		@Override
    		public void onClick(View v) {
    			Toast.makeText(target.getContext(), "Starting Download...", Toast.LENGTH_LONG).show();
    		}
    	});

    	b.setOnClickListener(new View.OnClickListener() {
    		@Override
    		public void onClick(View v) {
    			dialog.dismiss();

    		}
    	});

    }
    
    
    public void download(View view){
    	Toast.makeText(view.getContext(), "Starting Download...", Toast.LENGTH_LONG).show();
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.layout.menu, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.settings:
	 		     Intent in = new Intent();
	 		     in.setClass(this,Setting.class);
	 		     startActivity(in);
                return true;
            case R.id.info:
	 		     Intent intent = new Intent();
	 		     intent.setClass(this,Info.class);
	 		     startActivity(intent);
                return true;
            case R.id.signout:
	 		     Intent i = new Intent();
	 		     i.setClass(this,InfoPreserveActivity.class);
	 		     startActivity(i);
	 		     this.finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    
	public void onBackPressed(){
		return;
	}
}